﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiMVC.Html;
using EPiServer.Core;
using HtmlAgilityPack;
using Xunit;

namespace EPiMVC.Tests.DefaultPageListBuilderTests
{
    public class CreatePageListCalled
    {
        [Fact]
        public void ReturnsClosedUL()
        {
            DefaultPageListBuilder builder = new DefaultPageListBuilder();
            List<PageData> pages = new List<PageData>();

            string product = builder.CreatePageList(pages);

            HtmlNode html = HtmlNode.CreateNode(product);
            Assert.Equal<string>("ul", html.Name);
            Assert.True(html.Closed);
        }

        [Fact]
        public void WithSinglePage_ReturnsULWithSingleLI()
        {
            DefaultPageListBuilder builder = new DefaultPageListBuilder();
            List<PageData> pages = new List<PageData>();
            pages.Add(new PageData());

            string product = builder.CreatePageList(pages);

            HtmlNode html = HtmlNode.CreateNode(product);
            Assert.Equal<int>(1, html.ChildNodes.Count);
            Assert.Equal<string>("li", html.ChildNodes[0].Name);
        }

        [Fact]
        public void WithTwoPages_ReturnsULWithTwoLIs()
        {
            DefaultPageListBuilder builder = new DefaultPageListBuilder();
            List<PageData> pages = new List<PageData>();
            pages.Add(new PageData());
            pages.Add(new PageData());

            string product = builder.CreatePageList(pages);

            HtmlNode html = HtmlNode.CreateNode(product);
            Assert.Equal<int>(2, html.ChildNodes.Count);
            Assert.Equal<string>("li", html.ChildNodes[0].Name);
            Assert.Equal<string>("li", html.ChildNodes[1].Name);
        }

        [Fact]
        public void WithPages_ReturnedLIsContainLinkToPages()
        {
            DefaultPageListBuilder builder = new DefaultPageListBuilder();
            List<PageData> pages = new List<PageData>();
            pages.Add(new PageData());
            pages.Add(new PageData());

            string product = builder.CreatePageList(pages);

            HtmlNode html = HtmlNode.CreateNode(product);
            for (int i = 0; i < pages.Count; i++ )
            {
                Assert.Equal<int>(1, html.ChildNodes[i].ChildNodes.Count);
                Assert.Equal<string>("a", html.ChildNodes[i].ChildNodes[0].Name);
            }
        }
    }
}
